#include <regx52.h>
#include "max7219.h"
#include "clock.h"
#include "MatrixKey.h"
#include "Delay.h"

unsigned char TimeTickCount;	               //计时器计数
unsigned char timeHour,timeMin,timeSecond;  //时分秒

unsigned char isLockSetting=0,KeyNum=0;
char curLockSettingPos=0;

void main()
{
	unsigned char Dig1,Dig2,Dig4,Dig5,Dig7,Dig8;
	unsigned char max7219Map[10] = {0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b};
	
	Delay(1000);
	Max7219_Init();
	Clock_Init();
	while(1){
		
		KeyNum=MatrixKey();
		
		//开启设置
		if(KeyNum == 11 || isLockSetting == 1){
			isLockSetting =1;
		
			//显示当前位
			Max7219_Init();
			switch(curLockSettingPos)
			{
				case 0:
					Dig7 = timeHour%10;
					Dig8 = timeHour/10;
					WriteMax7219(Addr_Digit7, max7219Map[Dig7]);
					WriteMax7219(Addr_Digit8, max7219Map[Dig8]);
				break;
				case 1: 
					Dig4 = timeMin%10;
					Dig5 = timeMin/10;
					WriteMax7219(Addr_Digit5, max7219Map[Dig5]);
					WriteMax7219(Addr_Digit4, max7219Map[Dig4]);
				break;
				case 2:
					Dig1 = timeSecond%10;
					Dig2 = timeSecond/10;
					WriteMax7219(Addr_Digit1, max7219Map[Dig1]);
					WriteMax7219(Addr_Digit2, max7219Map[Dig2]);
				break;
			}
			
			KeyNum=MatrixKey();
			
			//关闭设置
			if(KeyNum == 12){
				isLockSetting =0;
				continue;
			}
		
			//左移
			if(KeyNum == 13){
				curLockSettingPos--;
				if(curLockSettingPos <= 0){
					curLockSettingPos = 0;
					continue;
				}
			}
			
			//右移
			if(KeyNum == 14){
				curLockSettingPos++;
				if(curLockSettingPos > 2){
					curLockSettingPos = 2;
					continue;
				}
			}
			
			//加1
			if(KeyNum == 15){
				switch(curLockSettingPos)
				{
					case 0:
						timeHour++;
						if(timeHour>=24){
							timeHour=0;
						}
					break;
					case 1:
						timeMin++;
						if (timeMin>=60) {
							timeMin=0;
							timeHour++;
							if(timeHour>=24){
								timeHour=0;
							}
						}
					break;
					case 2:
						timeSecond++;
						if (timeSecond>=60) {
							timeSecond = 0;
							timeMin++;
							if (timeMin>=60) {
								timeMin=0;
								timeHour++;
								if(timeHour>=24){
									timeHour=0;
								}
							}
						}
					break;
				}
				continue;
			}
			
			//减1
			if(KeyNum == 16){
				switch(curLockSettingPos)
				{
					case 0:
						timeHour--;
						if(timeHour < 0){
							timeHour = 24;
						}
					break;
					case 1:
						timeMin--;
						if (timeMin < 0) {
							timeMin = 60;
						}
					break;
					case 2:
						timeSecond--;
						if (timeSecond < 0) {
							timeSecond = 60;
						}
					break;
				}
			}
			continue;
		}
	};
}

/**
 * 定时器1每0.02秒中断1次,用于时钟计时
 */
void Timer1_Rourine() interrupt 3
{

	TH1  = 0xB8; 	 //给定时器赋初值 
	TL1  = 0x00; 	 //
	TimeTickCount++; //
	
	if(TimeTickCount >= 50){
		TimeTickCount = 0;
		timeSecond++;
		if (timeSecond>=60) {
			timeSecond = 0;
			timeMin++;
			if (timeMin>=60) {
				timeMin=0;
				timeHour++;
				if(timeHour>=24){
					timeHour=0;
				}
			}
		}
		
		//显示时钟
		if(isLockSetting==0){
			showClock(timeHour, timeMin, timeSecond);
		}
	}
}